import {Sequelize} from 'sequelize-typescript';
import config from '../config';
import { UserBasic } from './../models/user/userBasic';
import { UserCoinLog } from './../models/user/userCoinLog';
import { UserExpLevel } from './../models/user/userExpLevel';
import { UserExpLog } from './../models/user/userExpLog';
import { UserFollow } from './../models/user/userFollow';
import { TimeNodeBasic } from './../models/timenode/timeNodeBasic';
import { TimeNodeGroup } from './../models/timenode/timeNodeGroup';
import { TimeNodeType } from './../models/timenode/timeNodeType';
import { TravelBasic } from './../models/travel/travelBasic';

export default class Seq{

	sequelize:Sequelize;

	constructor(){

		this.sequelize = new Sequelize({
			database:config.mysql.database,
			dialect: "mysql",
			username: config.mysql.user,
			password: config.mysql.password,
			host: config.mysql.host,
			modelPaths:[__dirname+"/models"],
			define:{
				timestamps:false,
				paranoid:true,
				underscored:false,
				charset:'utf8',
				collate:'utf8_general_ci',
				freezeTableName:true
			}
		});

		// 建立连接
		this.sequelize.authenticate().then(function() {
			console.log("数据库连接成功");
		}).catch(function(err:any) {
			//数据库连接失败时打印输出
			console.error(err);
			throw err;
		});

		this.sequelize.addModels([
			UserBasic,
			UserCoinLog,
			UserExpLevel,
			UserExpLog,
			UserFollow,
			TimeNodeBasic,
			TimeNodeGroup,
			TimeNodeType,
			TravelBasic,
		]);
	}
}
